package SpLib.ObjSys
{
	import SpLib.Com.SingletonFactory;
	import SpLib.std.iterator;
	import SpLib.std.map;
	
	public class GlobalObjModule
	{
		private static var s_Instance:GlobalObjModule = null;
		private var m_szModule:SpLib.std.map = null;
		
		public static function GetInstance():GlobalObjModule
		{
			var name:String = "SpLib.ObjSys.GlobalObjModule";
			if(s_Instance==null)
			{
				s_Instance = SingletonFactory.Get(name) as SpLib.ObjSys.GlobalObjModule;
				if(s_Instance==null)
				{			
					s_Instance = new SpLib.ObjSys.GlobalObjModule();
					SingletonFactory.Reg(name,s_Instance);
				}
			}
						
			return s_Instance;
		}
		
		public static function Find(name:String):Object
		{
			var ins:GlobalObjModule = GetInstance();
			var iter:SpLib.std.iterator = ins.m_szModule.find(name);
			if(iter!=ins.m_szModule.end())
			{
				return iter.m_Data as Object;
			}
			
			return null;
		}
		
		public static function Reg(name:String,obj:Object):Boolean
		{
			var ins:GlobalObjModule = GetInstance();
			return ins.m_szModule.insert(name,obj);
		}
		
		public static function UnReg(name:String,obj:Object):void
		{
			var ins:GlobalObjModule = GetInstance();
			var iter:SpLib.std.iterator = ins.m_szModule.find(name);
			if(iter!=ins.m_szModule.end() && iter.m_Data==obj)
			{
				ins.m_szModule.erase(iter);
			}
		}
		
		public function GlobalObjModule()
		{
			if(SpLib.ObjSys.GlobalObjModule.s_Instance!=null)
			{
				throw new Error("GlobalObjModule is Singleton");
			}
			
			if(SingletonFactory.Get("SpLib.ObjSys.GlobalObjModule")!=null)
			{
				throw new Error("GlobalObjModule is Singleton");
			}
			
			m_szModule = new SpLib.std.map();
		}
	}
}